package com.tarena.lbs.basic.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.basic.dao.mapper.AreaMapper;
import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class AreaRepositoryImpl implements AreaRepository {
    @Autowired
    private AreaMapper areaMapper;

    // TODO 使用 Redis 缓存查询结果
    @Override
    public List<AreaPO> queryProvinces() {
        QueryWrapper<AreaPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        return areaMapper.selectList(queryWrapper);
    }

    @Override
    public List<AreaPO> queryChildren(Integer parentId) {
        QueryWrapper<AreaPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        return areaMapper.selectList(queryWrapper);
    }

    @Override
    public List<AreaPO> queryAll() {
        return areaMapper.selectList(new QueryWrapper<>());
    }

    //根据地址编码查询地址信息
    @Override
    public AreaPO selectById(Long adCode) {
        QueryWrapper<AreaPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ad_code", adCode);
        return areaMapper.selectOne(queryWrapper);
    }
}
